package com.udf.hive.udf.disturb;

import java.util.logging.Logger;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

/**
 * 掩码
 * 
 * @author Luyf
 * @version [版本号, 2021年2月18日]
 */
public class MaskUDF extends UDF
{
    private static Logger logger = Logger.getLogger("MaskUDF");

    public Text evaluate(Text data, String paramValue) {
        if (data != null) {
            String dataStr = data.toString();
            StringBuilder replaceStr = new StringBuilder();
            String[] params = paramValue.split(";");
            int start = Integer.parseInt(params[0]);
            int len = Integer.parseInt(params[1]);
            String maskCode = "*";
            if (params.length > 2) {
                maskCode = params[2];
            }
            for (int i = 0; i < len; i++) {
                replaceStr.append(maskCode);
            }
            if (start > dataStr.length() - 1) {
                return new Text(dataStr);
            }
            if ((start + len) > dataStr.length() - 1) {
                return new Text(dataStr.substring(0, start) + replaceStr.toString());
            }
            return new Text(dataStr.substring(0, start) + replaceStr.toString() + dataStr.substring(start + len));

        }
        return null;
    }

    public static void main(String[] args) {
        logger.info(String.valueOf(new MaskUDF().evaluate(new Text("123456"), "3;5")));
    }

}
